#include <stdarg.h>

void _vsnprintf(char* result, const char *format, va_list ap){
    int i = 0;
    while(*format != '\0'){
        if(*format == '%'){
            format++;
            if(*format == 'd'){
                long long int valint = va_arg(ap,long long int);
                char buf[256];
                int len = atoi(buf, valint, 10);
                int ptr = 0;
                while(ptr < len){
                    result[i] = buf[ptr];
                    i++;
                    ptr++;
                }
            }
            else if(*format == 's'){
                char* vastr = va_arg(ap,char *);
                while(*vastr != '\0'){
                    result[i] = *vastr;
                    i++;
                    vastr++;
                }
            }
            else if(*format == 'c'){
                char valint = va_arg(ap,int);
                result[i] = valint;
                i++;
            }
            else if(*format == 'f'){
                //todo
                // float valflt = va_arg(ap,double); 
                // char buf[256];
                // int len = atof(buf, valflt);
                // int ptr = 0;
                // while(ptr < len){
                //     str[i] = buf[ptr];
                //     i++;
                //     ptr++;
                // }
            }
            else if(*format == 'x'){
                unsigned long long int valint = va_arg(ap, unsigned long long int);
                char buf[256];
                int len = atoi(buf, valint, 16);
                int ptr = 0;
                result[i++] = '0';
                result[i++] = 'x';

                result[i] = buf[ptr];
                i++;
                ptr++;
                while(ptr < len){
                    if((ptr%4) == (len%4)){
                        result[i++] = '_';
                    }
                    result[i] = buf[ptr];
                    i++;
                    ptr++;
                }
            }
            else if(*format == 'b'){
                unsigned long long int valint = va_arg(ap,unsigned long long int);
                char buf[256];
                int len = atoi(buf, valint, 2);
                int ptr = 0;
                result[i++] = '0';
                result[i++] = 'b';

                result[i] = buf[ptr];
                i++;
                ptr++;
                while(ptr < len){
                    if((ptr%8) == (len%8)){
                        result[i++] = '_';
                    }
                    result[i] = buf[ptr];
                    i++;
                    ptr++;
                }
            }
            else{
                result[i++] = '%';
                result[i++] = *format;
            }
        }
        else{
            result[i] = *format;
            i++;
        }

        format++;

    }
    result[i] = '\0';
}

void vsnprintf(char* result, const char *format, ...){
    va_list ap;  
    va_start(ap,format);     //将ap指向第一个实际参数的地址
    _vsnprintf(result, format, ap);
    va_end(ap);
}